﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EraNet.Core;
using EraNet.Core.Model.Messages;
using EraNet.Core.Data;
using EraNet.Core.Caching;

namespace EraNet.Services.Messages
{
    public class FeedbackServices : IFeedbackServices
    {
        #region Constants
        private const string FEEDBACK_BY_ID_KEY = "EraNet.Feedback.Id-{0}";
        #endregion
        #region Ctor
        private readonly IRepository<Feedback> _feedbackRepository;
        private readonly ICacheManager _cacheManager;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="adminRepository"></param>
        /// <param name="cacheManager"></param>
        public FeedbackServices(IRepository<Feedback> feedbackRepository, ICacheManager cacheManager)
        {
            _feedbackRepository = feedbackRepository;
            _cacheManager = cacheManager;
        }
        #endregion
        #region Method
        public int Insert(Feedback model)
        {
            if (model == null) throw new ExtException("NUll Feedback Model");
            int rs = _feedbackRepository.Insert(model);
            return rs;
        }

        public int Update(Feedback model)
        {
            if (model == null) throw new ExtException("Null Feedback Model");
            return _feedbackRepository.Update(model) > 0 ? 1 : 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg"></param>
        /// <returns>0，失败 1，成功 3，内容太长</returns>
        public int Replay(Feedback model,string msg)
        {
            if (model == null) return 0;
            if (string.IsNullOrEmpty(msg)) return 0;
            if (msg.Length > 500) return 3;
            string con = model.Contents;
            return _feedbackRepository.Update(model) > 0 ? 1 : 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(Feedback model)
        {
            if (model == null) return 0;
            return _feedbackRepository.Delete(model) > 0 ? 1 : 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="feedbackId"></param>
        /// <returns></returns>
        public Feedback GetNoticeById(int feedbackId)
        {
            if (feedbackId <= 0) return null;
            return _feedbackRepository.GetById(feedbackId);
        }

        public IPagedList<Feedback> SelectPagerByUser(int userId, int pageIndex, int pageSize)
        {
            var query = _feedbackRepository.Table;
            query = query.Where(c => c.MemberId == userId && c.feedbackType != 4).OrderBy(c => c.CreateTime);
            if (pageIndex < 1) pageIndex = 1;
            pageIndex = pageIndex - 1;
            var list = new PagedList<Feedback>(query, pageIndex, pageSize);
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SchoolId"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Pagesize"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IPagedList<Feedback> SelectPager(int SchoolId, int pageIndex, int pageSize)
        {
            var query = _feedbackRepository.Table;
            query = query.Where(c=>c.Recipient==SchoolId&&c.feedbackType!=4).OrderBy(c => c.CreateTime);
            if (pageIndex < 1) pageIndex = 1;
            pageIndex = pageIndex - 1;
            var list = new PagedList<Feedback>(query, pageIndex, pageSize);
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="SchoolId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="IsShow"></param>
        /// <returns></returns>
        public IPagedList<Feedback> SelectPager(int SchoolId, int pageIndex, int pageSize,bool IsShow)
        {
            var query = _feedbackRepository.Table;
            if (!IsShow) query = query.Where(c => c.Status == 0);
            query = query.Where(c => c.Recipient == SchoolId && c.feedbackType != 4).OrderBy(c => c.CreateTime);
            if (pageIndex < 1) pageIndex = 1;
            pageIndex = pageIndex - 1;
            var list = new PagedList<Feedback>(query, pageIndex, pageSize);
            return list;
        }
        #endregion
    }
}
